package com.psa.dialog;

import android.content.Context;
import com.psa.component.loading.UIHelper;
import com.wxw.BaseLibrary.tool.Timeout;

/**
 *
 * @author david
 * @date 2015-12-17
 */
public class WaitingDialog {
    private Timeout timeout = null;
    private Timeout.OnTimeoutListener onTimeoutListener = null;


    public WaitingDialog(Context context, String msg, int timeout, Timeout.OnTimeoutListener onTimeoutListener) {
        this(context, "", msg, timeout, onTimeoutListener);
    }

    public WaitingDialog(Context context, String title, String msg, int timeout, Timeout.OnTimeoutListener onTimeoutListener) {
        this(context, title, msg, timeout, false, onTimeoutListener);
    }

    public WaitingDialog(Context context, String title, String msg, int timeout, boolean cancelable, Timeout.OnTimeoutListener onTimeoutListener) {
        UIHelper.showDialogForLoading(context, title, msg, cancelable);
        this.onTimeoutListener = onTimeoutListener;
        this.timeout = new Timeout(timeout);
        this.timeout.setOnTimeoutListener(new Timeout.OnTimeoutListener() {
            @Override
            public void onTimeout(Object sender, int time) {
                UIHelper.hideDialogForLoading();
                if (WaitingDialog.this.onTimeoutListener!=null) {
                    WaitingDialog.this.onTimeoutListener.onTimeout(sender, time);
                }
            }
        });
        this.timeout.start();
    }

    public void stop() {
        if (timeout != null) {
            timeout.stop();
        }
        UIHelper.hideDialogForLoading();
    }
}
